package Servlet;


import entity.User;
import util.SimpleBeanConvertor;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;

@WebServlet("/test")
public class TestServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
//        resp.set("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        this.doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        //        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        try {
// SimpleBeanConvertor.convert将请求req对象中的请求参数，在调用User实体类中的相关set方法后，赋值给User实体类中相关属性
            User user = SimpleBeanConvertor.convert(req, User.class);
            System.out.println(user);
            resp.getWriter().print(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
